<template>
  <el-button type="primary" @click="handleClick">
    <slot>{{ title }}</slot>
  </el-button>
  <el-dialog
    :title="title"
    v-model="dialogVisible"
  >
    <div class="container">
      <div
        class="item"
        v-for="(item, index) in Object.keys(ElIcons)"
        :key="index"
        @click="clickItem(item)"
      >
        <component :is="`el-icon-${toLine(item)}`" />
        <div>{{ item }}</div>
      </div>
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import * as ElIcons from '@element-plus/icons'
import { toLine } from '@/utils';
import { useCopy } from '@/hooks/useCopy'

defineOptions({
  name: 'ChooseIcon'
})
const props = defineProps<{
  title: string;
  visible: boolean;
}>()
const emits = defineEmits(['update:visible'])
const dialogVisible = ref<boolean>(props.visible)
const handleClick = () => {
  emits('update:visible', !props.visible)
}
const clickItem = (item: string) => {
  const text = `<el-icon-${toLine(item)} />`
  useCopy(text)
  dialogVisible.value = false
}

watch(() => props.visible, val => {
  dialogVisible.value = val
})

watch(() => dialogVisible.value, val => {
  emits('update:visible', val)
})
</script>

<style scoped>
.container {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}

.item {
  width: 20%;
  height: 70px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
  cursor: pointer;
}

svg {
  width: 2em;
  height: 2em;
}
</style>
